package com.winit.openapi.wh.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.inbound.InboundDutiableProveDataFileService;
import com.winit.oms.spi.inbound.command.InboundDutiableProveDataFileCommand;
import com.winit.oms.spi.inbound.vo.InboundDutiableProveDataFileVo;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.builder.InboundDutiableProveDataFileBuilder;

/**
 * 直发完税文件上传
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * youming.fan 	1.0  		2017-11-13 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/dutiableProveDataFile")
public class InboundDutiableProveDataFileController extends BaseController {

	@Resource
	private InboundDutiableProveDataFileService inboundDutiableProveDataFileService;

	/**
	 * 新增
	 * 
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
	@RequestMapping(value = "/addDataFile")
	public String addInboundDutiableProveDataFile(
			@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		InboundDutiableProveDataFileCommand command = new InboundDutiableProveDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validNotNull(json, "orderNo");
			command.setWinitOrderNo(json.getString("orderNo"));
			command.setFileUrl(json.getString("fileUrl"));
			command.setFileName(json.getString("fileName"));
			command.setCtx(CommandContext.getContext());
			InboundDutiableProveDataFileVo vo = inboundDutiableProveDataFileService
					.addInboundDutiableProveDataFile(command);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("inboundCustomsDataFileVo",
					InboundDutiableProveDataFileBuilder
							.buildInboundDutiableProveDataFileMap(vo));
			responseMsg.setData(data);
		}
		return SUCCESS;
	}

	@RequestMapping(value = "/deleteFileById")
	public String deleteInboundDutiableProveDataFileById(
			@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		InboundDutiableProveDataFileCommand command = new InboundDutiableProveDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validNotNull(json, "orderNo");
			ValidateUtil.validNotNull(json, "id");
			command.setWinitOrderNo(json.getString("orderNo"));
			if (json.containsKey("id")) {
				command.setId(json.getLong("id"));
			}
			command.setCtx(CommandContext.getContext());
			String orderNo = inboundDutiableProveDataFileService
					.deleteInboundDutiableProveDataFileById(command);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("orderNo", orderNo);
			responseMsg.setData(data);
		}
		return SUCCESS;
	}

	@RequestMapping(value = "/deleteFileByUrl")
	public String deleteInboundDutiableProveDataFileByCustomsDataUrl(
			@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		InboundDutiableProveDataFileCommand command = new InboundDutiableProveDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validNotNull(json, "orderNo");
			ValidateUtil.validNotNull(json, "fileUrl");
			command.setWinitOrderNo(json.getString("orderNo"));
			command.setFileUrl(json.getString("fileUrl"));
			command.setCtx(CommandContext.getContext());
			String orderNo = inboundDutiableProveDataFileService
					.deleteInboundDutiableProveDataFileByCustomsDataUrl(command);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("orderNo", orderNo);
			responseMsg.setData(data);
		}
		return SUCCESS;
	}

	@RequestMapping(value = "/queryByOrderNo")
	public String queryByWinitOrderNo(@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		InboundDutiableProveDataFileCommand command = new InboundDutiableProveDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validNotNull(json, "orderNo");
			command.setWinitOrderNo(json.getString("orderNo"));
			command.setCtx(CommandContext.getContext());
			List<InboundDutiableProveDataFileVo> list = inboundDutiableProveDataFileService
					.queryByWinitOrderNo(command);
			List<Map<String, Object>> rslist = new ArrayList<Map<String, Object>>();
			Map<String, Object> map = null;
			for (InboundDutiableProveDataFileVo vo : list) {
				map = InboundDutiableProveDataFileBuilder
						.buildInboundDutiableProveDataFileMap(vo);
				rslist.add(map);
			}
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("list", rslist);
			responseMsg.setData(data);
		}
		return SUCCESS;
	}
	
	@RequestMapping(value = "/queryIsAvailable")
	public String queryIsAvailableByWarehouseCode(@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg){
		InboundDutiableProveDataFileCommand command = new InboundDutiableProveDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validNotNull(json, "warehouseCode");
			command.setWarehouseCode(json.getString("warehouseCode"));
			command.setCtx(CommandContext.getContext());
			String isAvailable = inboundDutiableProveDataFileService
					.queryIsAvailableDataFileByWarehouseCode(command);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("isAvailable", isAvailable);
			responseMsg.setData(data);
		}
		return SUCCESS;
	}
}
